package com.jojo.jojomall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jojo.jojomall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jojo.common.utils.PageUtils;
import com.jojo.common.utils.Query;

import com.jojo.jojomall.product.dao.BrandDao;
import com.jojo.jojomall.product.entity.BrandEntity;
import com.jojo.jojomall.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
@Autowired
private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //获取key进行模糊查询；
        /*首先获取prarms里的key值*/
        String key = (String) params.get("key");
        LambdaQueryWrapper<BrandEntity> brandQueryWrapper = new LambdaQueryWrapper<>();
    if (!StringUtils.isEmpty(key)) {//如果查找关键字key不为空
        //则在该加的地方加上这个
        brandQueryWrapper.eq(BrandEntity::getBrandId, key).or().like(BrandEntity::getName, key);
    }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                brandQueryWrapper
        );
        return new PageUtils(page);
    }
//因为要改变品牌表，必须同时改变依附的关联表
    @Override
    public void updateDetail(BrandEntity brand) {
        //首先将自己的更新做了
        this.updateById(brand);
        //其次查看品牌名是否为空
        if (!StringUtils.isEmpty(brand.getName())){
            //不为空则要修改关联表中关联的品牌名对应

        categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
        }
    }
}
